package com.ruoyi.web.controller.warning;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.warning.domain.entity.SysRoleDevice;
import com.ruoyi.warning.service.ISysRoleDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 角色和菜单关联表 前端控制器
 * </p>
 *
 * @author chenhc
 * @since 2025-07-28
 */
@RestController
@RequestMapping("/sysRoleDevice")
@Api(tags = "角色设备权限", description = "用于处理角色设备相关操作的控制器")
public class SysRoleDeviceController {

    @Resource
    private ISysRoleDeviceService sysRoleDeviceService;

    @ApiOperation("获取角色的设备权限")
    @GetMapping(value = "/getList/{roleId}")
    public R getList(@PathVariable Long roleId)
    {
        return R.ok(sysRoleDeviceService.getList(roleId));
    }

    @ApiOperation("新增或修改角色权限")
    @PostMapping(value = "/saveOrUpdate")
    public R saveOrUpdate(@RequestBody List<SysRoleDevice> list)
    {
        this.delete(list.get(0).getRoleId());
        sysRoleDeviceService.saveBatch(list);
        return R.ok("操作成功");
    }

    @ApiOperation("删除角色权限")
    @GetMapping(value = "/delete/{roleId}")
    public R delete(@PathVariable Long roleId)
    {
        sysRoleDeviceService.remove(new LambdaQueryWrapper<SysRoleDevice>().eq(SysRoleDevice::getRoleId, roleId));
        return R.ok("操作成功");
    }
}
